package www.my.imageframecompare.oom;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.load.engine.Resource;

import java.util.ArrayList;
import java.util.List;

import www.my.imageframecompare.R;

public class BigImgActivity extends AppCompatActivity {
    private static final String TAG = "BigImgActivity";
    private ImageView iV320;
    private LinearLayout llContainer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_big_img);
        iV320 = (ImageView) findViewById(R.id.iv_320);
        llContainer = (LinearLayout) findViewById(R.id.ll);
        getMaxMemory();
        //一定要 max  否则 getWidth() 为0;
        Log.i(TAG, "onCreate: w  "+iV320.getMaxWidth()+"---h "+iV320.getMaxHeight());
        Log.i(TAG, "onCreate: w  "+iV320.getMaxWidth()+"---h "+iV320.getMaxHeight());
        iV320.setImageBitmap(decodeSampleBitmapFromResource(getResources(),
                R.mipmap.ic_man,iV320.getMaxWidth(),iV320.getMaxHeight()));
        List<String> imgList=new ArrayList<>();
        dynamicAddImg(llContainer,imgList);
    }

    private void dynamicAddImg(@NonNull ViewGroup group,@NonNull List<String> imgList) {
        //必须src 添加img 否则 模糊
        final ImageView iv = (ImageView) LayoutInflater.from(this).inflate(R.layout.item_dynamic_img, null, false);


    }

    //得到 实际图片宽高
    private void bitMapFactoryPress() {
        BitmapFactory.Options options = new BitmapFactory.Options();
        //inJustDecodeBounds属性设置为true就可以让解析方法禁止为bitmap分配内存，
        // 返回值也不再是一个Bitmap对象，而是null。
        // 虽然Bitmap是null了，但是BitmapFactory.Options的outWidth、outHeight和outMimeType属性都会被赋值
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(getResources(), R.mipmap.ic_man, options);
        int imageHeight = options.outHeight;
        int imageWidth = options.outWidth;
        String imageType = options.outMimeType;
    }

    //计算原图/展示imageview 比率
    public int calculateInSampleSize(BitmapFactory.Options options,int reqWidth,int reqHeight){
        // 源图片的高度和宽度
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;
        if (height > reqHeight || width > reqWidth) {
            // 计算出实际宽高和目标宽高的比率
            final int heightRatio = Math.round((float) height / (float) reqHeight);
            final int widthRatio = Math.round((float) width / (float) reqWidth);
            // 选择宽和高中最小的比率作为inSampleSize的值，这样可以保证最终图片的宽和高
            // 一定都会大于等于目标的宽和高。
            inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
        }
        return inSampleSize;
    }

    //压缩图片
    public Bitmap decodeSampleBitmapFromResource(@NonNull Resources res, int resid, int reqWidth, int reqheight){
        // 第一次解析将inJustDecodeBounds设置为true，来获取图片大小
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds=true;
        BitmapFactory.decodeResource(res,resid,options);
        // 调用上面定义的方法计算inSampleSize值
        options.inSampleSize  = calculateInSampleSize(options, reqWidth, reqheight);
        // 使用获取到的inSampleSize值再次解析图片
        options.inJustDecodeBounds=false;
        return BitmapFactory.decodeResource(res,resid,options);
    }


    private void getMaxMemory() {
        int maxMemory  = (int) (Runtime.getRuntime().maxMemory() / 1024);
        Log.i(TAG, "getMaxMemory最大内存: ==="+maxMemory +"kb"  +(maxMemory/1024)  +"mb");
    }
}
